package com.ts.api.http.user.phone;

import com.alibaba.fastjson2.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.StringUtils;
import com.gitee.apanlh.util.log.LogLevel;
import com.gitee.apanlh.util.net.http.HttpClientBuilder;
import com.gitee.apanlh.util.net.http.HttpResponse;
import com.gitee.apanlh.web.http.HttpMethod;
import com.ts.api.module.common.entity.bo.CheckPhoneStatusBO;
import org.springframework.stereotype.Component;


/**
 * 金数天舟
 *
 * @author WJB
 */
@Component
public class JstzPhoneStatusProvider implements UserPhoneAuthProvider {

    public static final String CHECK_PHONE_URL = "https://api.chinadatapay.com/communication/personal/2110";
    public static final String KEY = "63faec67f138ddf4fc47edd02b8d500a";

    @Override
    public CheckPhoneStatusBO checkPhoneStatus(String phone) {
        HttpResponse response = HttpClientBuilder.builder(StringUtils.format(CHECK_PHONE_URL), HttpMethod.POST)
            .withHeader("Accept", "application/json;charset=utf-8")
            .withHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
            .withBodyForm(form -> {
                form.put("key", KEY);
                form.put("mobile", phone);
            })
            .withLogLevel(LogLevel.WARN)
            .withErrorLogLevel(LogLevel.WARN)
        .build();

        String str = response.getStr();
        JSONObject jsonObject = JSONObject.parseObject(str);
        String code = jsonObject.getString("code");
        JSONObject data = jsonObject.getJSONObject("data");

        //  成功状态
        if (Eq.object("10000", code)) {
            CheckPhoneStatusBO checkPhoneStatusBO = new CheckPhoneStatusBO();
            String state = data.getString("state");
            String operatorReal = data.getString("operator_real");

            //  根据三方接口定义来修改自己数据库中的状态-变更为用户信息表运营商状态及手机号码状态（金数天舟）
            //  2:正常
            //  3:停机
            //  5:不在网（空号）
            //  6:关机
            //  7:忙音
            //  8:关机/开机呼叫转移（移动返回全状态，联通/电信返回部分状态）
            ChooseEq.create(state)
                .when("2", () -> checkPhoneStatusBO.setPhoneStatus("0"))
                .when("3", () -> checkPhoneStatusBO.setPhoneStatus("2"))
                .when("5", () -> checkPhoneStatusBO.setPhoneStatus("1"))
                .when("6", () -> checkPhoneStatusBO.setPhoneStatus("5"))
                .when("7", () -> checkPhoneStatusBO.setPhoneStatus("4"))
                .when("8", () -> checkPhoneStatusBO.setPhoneStatus("5"))
            .end();

            //  1	移动
            //  2	联通
            //  3	电信
            ChooseEq.create(operatorReal)
                .when("1", () -> checkPhoneStatusBO.setCarrier("1"))
                .when("2", () -> checkPhoneStatusBO.setCarrier("2"))
                .when("3", () -> checkPhoneStatusBO.setCarrier("0"))
            .end();
            return checkPhoneStatusBO;
        }

        //  返回检测异常
        CheckPhoneStatusBO checkPhoneStatusBO = new CheckPhoneStatusBO();
        checkPhoneStatusBO.setPhoneStatus("100");
        checkPhoneStatusBO.setCarrier("3");
        return checkPhoneStatusBO;
    }
}
